﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using uAccess;
using WebApi.Models;

namespace WebApi.Services
{
    public class MyOrders
    {

        //显示所有订单
        public static JArray OrdersList()
        {
            JArray orders = new JArray();
            var op = new uAccess<Orders>("dbConnect").GetAll();
            var orders_str = JsonConvert.SerializeObject(op);
            orders = JArray.Parse(orders_str);
            return orders;
        }

        //显示某一个订单信息
        public static JObject GetOrder(string ord_id)
        {
            ord_id = ord_id.Substring(1, ord_id.Length - 2);//json传过来时会将"转义为"\，所以要进行字符截取
            uAccess<Orders> op = new uAccess<Orders>("dbConnect");
            var pr = op.GetFirst(c => c.Odr_Id == ord_id);
            string state;
            if(pr.Odr_State==0)
            {
                state = "未发货";
            }
            else if(pr.Odr_State == 1)
            {
                state = "未发货";
            }
            else if (pr.Odr_State == 2)
            {
                state = "未发货";
            }
            else if (pr.Odr_State == 3)
            {
                state = "未发货";
            }
            else if (pr.Odr_State == 4)
            {
                state = "未发货";
            }
            JObject goodObj = new JObject
                {
                    {"odr_id",pr.Odr_Id},
                    {"sku_id",pr.SKU_Id},
                    {"pro_id",pr.pro_Id},
                    {"c_id",pr.C_Id},
                    {"ord_transactionid",pr.Odr_TransactionId},
                    {"ord_time",pr.Odr_TimeStamp},
                    {"ord_parent",pr.Odr_Parent},
                    {"ord_amount",pr.Odr_Amount},
                    {"ord_unitprice",pr.Odr_UnitPrice},
                    {"ord_totalprice",pr.Odr_TotalPrice},
                    {"ord_state",pr.Odr_State},
                    {"ord_commission",pr.Odr_Commission},
                    {"ord_transitnum",pr.Odr_TransitNum},
                    {"ord_count",pr.Odr_Count},
                    {"co_id",pr.Co_Id},
                    {"ord_receipt",pr.Odr_Receipt},
                    {"ord_buyer",pr.Odr_Buyer},
                    {"ord_address",pr.Odr_Address},
                    {"ord_phone",pr.Odr_Phone},
                    {"ord_iscommented",pr.Odr_IsCommented},

                };

            return goodObj;
        }

        //模糊查询
        public static JArray FuzzySearch(String SearchText)
        {
            JArray customers = new JArray();
            var op = new uAccess<Orders>("dbConnect"); //数据库连接对象
            var customers_str = JsonConvert.SerializeObject(op.GetAll(c => c.Odr_Id.Contains(SearchText)));
            customers = JArray.Parse(customers_str);
            return customers;
        }
    }
}
